<script>	
	function checkDublicateName()
	{	var k=0;
				for (i=0;i<=400;i=i+6)
					{
					if($("#0"+i).length>0)
					{		$k=0;
							 for (j=0;j<=400;j=j+6)
							{
							if($("#0"+j).length>0 && $("#0"+i).val()==$("#0"+j).val())
							{
							 $k++;
							 
							}
							}
						if($k>1)
					{
					alert("Dublicate stock Entry. please remove new and add stock in existing one !");
					
					}
				 	 
					}
					}				
	}
	function callAutoAsignValue(idname)
	{
			
			 var name1 = parseInt(idname,10);
			 var quantity1 = name1+1;
			 var brate1 =  quantity1+1;
			 var srate1 =  brate1+1;
			 var avail1 = srate1+1;
			 var total1 = avail1+1;
			
			 if(parseInt(idname)>0)
			 {
			 quantity1="00"+quantity1;
			 brate1="000"+brate1;
			 srate1="0000"+srate1;
			 avail1="00000"+avail1;
			 total1="000000"+total1;
			 
			 }
			 else
			 {
			  quantity1="00";
			  brate1="000";
			  srate1="0000";
			  avail1="00000";
			  total1="000000";
			  
			 }
				 $.post('sales_check_details.php', {stock_name: $("#"+idname).val() },
				function(data){
								$("#"+brate1).val(data.brate);
								$("#"+srate1).val(data.rate);
								$("#"+avail1).val(data.availstock);
								$("#"+quantity1).focus();
							}, 'json');
							
						checkDublicateName();	
							
	}
	function callQKeyUp(Qidname)
	{		
	
			
			 
			 var quantity = parseInt(Qidname,10);
			 var brate =  quantity+1;
			 var srate =  brate+1;
			 var avail = srate+1;
			 var total = avail+1;
			 var rowcount = parseInt((total+1)/5);
			 if(rowcount==0)
			 rowcount=1;
			
			 if(parseInt(Qidname)>0)
			 {
			 quantity="00"+quantity;
			 brate="000"+brate;
			 srate="0000"+srate;
			 avail="00000"+avail;
			 total="000000"+total
			 }
			 else
			 {
			  quantity="00";
			  brate="000";
			  srate="0000";
			  avail="00000";
			  total="000000";
			  
			  
			 }
			var result= parseFloat($("#"+quantity).val()) * parseFloat( $("#"+brate).val() );
			result=result.toFixed(2);
			$("#"+total).val(result);
			updateSubtotal();
			
	}
	function balanceCalc()
	{		if(parseFloat($("#payment").val()) > parseFloat($("#subtotal").val()))
			$("#payment").val(parseFloat($("#subtotal").val()));
			
			var result= parseFloat($("#subtotal").val()) - parseFloat( $("#payment").val() );
			result=result.toFixed(2);
			$("#balance").val(result);
	}
	function updateSubtotal()
	{					
					var temp=0;
					for (i=5;i<=400;i=i+6)
					{
					if($("#000000"+i).length>0)
					{
					 temp=parseFloat(temp)+parseFloat($("#000000"+i).val());
				 	 
					}
					}
				
			
			var subtotal=parseFloat(temp);
			
			if($("#000000").length>0)
			{
			var firstrowvalue=$("#000000").val();
			
			subtotal=parseFloat(subtotal)+parseFloat(firstrowvalue);
			}
			subtotal=subtotal.toFixed(2);
			$("#subtotal").val(subtotal);
			
			
	}
	
	function callRKeyUp(Ridname)
	{
			 var brate = parseInt(Ridname,10);
			 var quantity =  brate-1;
			 var srate =  brate+1;
			 var avail = srate+1;
			 var total = avail+1;
			 
			 callQKeyUp(brate-1)
	}
	</script>
    
<?php 
	$stocknumber=$billnumber = $date =$supplier =$name=$quantity=$brate=$srate=$avail=$total=$payment=$balance=$subtotal=$mode= "";
	$control ="insert";
	$btn = " Sell "; 
	
	if(isset($_REQUEST['control'])){
		$payment=$_POST['payment'];
		$supplier=$_POST['supplier'];
		$name=$_POST['name'];
		if($supplier==""){
			errorMessage("Please select supplier...!");
		}
		elseif($payment==""){	
			errorMessage("Please input Payment...!");
		}
		else{
			include("add_stock_verify.php");
		}
	}
?>
    			<table class="bordered" width="770">
                    <thead>
                        <tr>
                            <th>Please insert your Stock information:<br /><span class="red">* require feild</span></th>
                        </tr>
                        <tr>
                        	<?php 
							  if(@$errors!=""){
							  	echo " <td><span class='red'>". @$errors."</span></td>";
							  }else{
								  if(isset($_REQUEST['msgStatus']))
									echo " <td><span class='red'>". $_REQUEST['msgStatus'].@$_REQUEST['one']."</span></td>";
							  }
							?>
                           
                        </tr>
                    </thead>
				</table>
				<form class="form" method="post"  action="index.php?page=add_stock_new&action=add_stock_new&control=<?php echo $control;?>" name="form1" id="form1"    onSubmit="updateSubtotal()"> <!-- onSubmit="updateSubtotal()" onSubmit="return validateForm()" -->
                  <table width="842"  id="dynamictable" style="margin:20px 0px 0px 0px;">
                  		<tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td width="68">&nbsp;</td>
                       </tr> 
                        <tr>
                        	<td width="67"><span class="big">Stock ID</span> </strong>
                              <?php 
								$maxOfAll=maxOfAll("id","tbl_stocks");
								$maxOfAll=$maxOfAll + 1;
								$stockNumber="SE" .$maxOfAll ;
						      ?>   
                          </td>
                          <td width="113"><input name="stocknumber" type="text" id="saleNumber" readonly value="<?php echo @$stockNumber; ?>" style="width:50px;"></td>
                          <?php 
							$maxOfAll=maxOfAll("id","tbl_stocks");
							$maxOfAll=$maxOfAll + 1;
							$billNumber="B" .$maxOfAll ;
						  ?>
                          <td width="61"><span class="big">Bill No </span></td>
                          <td width="129"><input type="text" name="billnumber" style="width:60px;" readonly value="<?php echo @$billNumber;?>" id="billnumber" class="validate[required,length[0,100]] text-input"></td>
                          <td width="42"><span class="big">Date</span></td>
                          <td width="113"><input type="text" id="date" name="date" class="date_input" value="<?php echo date('d-m-Y');?>" style="width:70px;"></td>
                          <td width="59"><span class="big">Supplier</span></td>
                          <td width="150">
                          		<select name="supplier" class="input req-min" minlength="1" style="width:150px; height:25px;" >
                                     <option value=""><--Please Supplier--></option>
                                        <?php
											@$supplier=$_REQUEST['msgStatus'];
                                            $sql="SELECT * FROM tbl_suppliers ORDER BY id DESC";
                                            $result=query($sql);
                                            while($row=fetch_object($result))
                                            {
												if(@$supplier == $row->sup_name)
													$selected = " selected='selected'";
												else	
													$selected = "";
                                        ?>
                                      <option value="<?php echo $row->sup_name;?>"<?php echo $selected;?>><?php echo $row->sup_name;?></option>
                                        <?php 
                                            }
                                        ?>
                          		</select>
                          </td>
                          <td><div class="new"><a href="index.php?page=supplier_list&action=supplier_addnew">New</a></div></td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                       </tr> 
               </table>
			   <table width="800"   id="duplicate" style="margin:20px 0px 0px 0px;">
  					<tr>
                       <td ><span class="smal">Product</span></td>
                       <td >
                          <select name="name[]" id="0" onblur="callAutoAsignValue(this.id)" autocomplete="off" class="text_smal"   >
                                 <option value=""><--Please product--></option>
                                    <?php
										$sql="SELECT * FROM tbl_products ORDER BY pd_id DESC";
										$result=query($sql);
										while($row=fetch_object($result))
										{
                                    ?>
                                  <option value="<?php echo $row->pd_name;?>"><?php echo $row->pd_name;?></option>
                                    <?php 
										}
									?>
                          </select>
                      </td>
                      <td ><div class="new"><a href="index.php?page=products&action=products_addnew_stock">New</a></div></td>
                      <td><span class="smal">Qty</span></td>
                      <td><input name="quantity[]" class="input req-min" minlength="1" type="text" id="00"  style="width:50px;" onKeyUp="callQKeyUp(this.id)"></td>
                      <td><span class="smal">Buy Rate</span></td>
                      <td><input name="brate[]" class="input req-min" minlength="1" type="text" id="000" style="width:50px;" onKeyUp="callRKeyUp(this.id)"  ></td>
					  <td><span class="smal">Sale Rate</span> </td>
					  <td><input name="srate[]" class="input req-min" minlength="1" type="text" id="0000"  style="width:50px;"  ></td>
					  <td><span class="smal">Avail</span></td>
						<td><input name="avail[]" class="input req-min" minlength="1" type="text" id="00000" readonly value="" style="width:50px;" ></td>
                      <td><span class="smal">Total</span></td>
                      <td><input name="total[]" class="input req-min" minlength="1" type="text" id="000000" readonly value="" style="width:70px;text-align:right;" >  </td>
                      <td width="50"><p><span><a id="minus" href=""  >[-]</a> <a id="plus" href="">[+]</a></span></p></td>
                 </tr>
			 </table>
			 <table width="700"  border="0" align="center" cellpadding="0" cellspacing="0"  id="duplicate" style="margin:20px 0px 0px 0px;">
             	  <tr>
              			<td>&nbsp;</td>
					    <td>&nbsp;</td>
					    <td width="80">&nbsp;</td>
					    <td width="106">&nbsp;</td>
					    <td width="93">&nbsp;</td>
					    <td width="145">&nbsp;</td>
					    <td width="50">&nbsp;</td>
			      </tr>
                  <tr>
                            <td width="80"> <span class="big">Payment:</span> <span class="red">*</span></td>
                            <td><input type="text" class="input req-min" minlength="1" name="payment" style="width:100px; " id="payment"   onKeyUp="balanceCalc()"></td>
                            <td><span class="big">Balance:</span></td>
                            <td><input name="balance" type="text"  id="balance" style="width:100px;" value="0.00" readonly></td>
                            <td><div align="center"><span class="big">Sub Total</span></div></td>
                            <td><input name="subtotal" id="subtotal" type="text" readonly style="width:100px; text-align:right; color:#333333; font-weight:bold; font-size:16px;"><img src="images/refresh.png" alt="Refresh" align="absmiddle" onClick="updateSubtotal()" style="cursor:pointer;"></td>		
                  </tr>
                  <tr>
              			<td>&nbsp;</td>
					    <td>&nbsp;</td>
					    <td>&nbsp;</td>
					    <td>&nbsp;</td>
					    <td>&nbsp;</td>
					    <td>&nbsp;</td>
					    <td>&nbsp;</td>
			      </tr>
                  
                  <tr>
                            <td width="80"><span class="big">Mode:</span> <span class="red">*</span></td>
                            <td width="146">
                              <select name="mode">
                                <option value="cheque">Cheque</option>
                                <option value="cash" selected>Cash</option>
                                <option value="others">others</option>
                              </select>
                            </td>
                            <td ><span class="big">Next Due</span></td>
                            <td ><input type="text" id="lastupdate" name="lastupdate" class="date_input" value="<?php echo date('d-m-Y');?>" style="width:70px;"></td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                  </tr>
                  <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                  </tr>
                  <tr>
                  			<td >
                               
                            </td >
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td colspan="2"> <input class="btnSubmit" type="submit" id="Submit" name="Submit" value="Save" onClick="updateSubtotal()" >
                                <input class="btnSubmit" type="button" name="Reset" value="Cancel" onclick="redirect('index.php?page=add_stock&action=add_stock')">
                            </td>
                 </tr>
		</table>
                </form>
           


